<template>
    <el-dialog
            :title="!dataForm.cleaningId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="数据清洗ID" prop="cleaningId">
                        <el-input v-model.number="dataForm.cleaningId" placeholder="数据清洗ID"></el-input>
                </el-form-item>
                <el-form-item label="关联的数据采集ID" prop="collectionId">
                        <el-input v-model.number="dataForm.collectionId" placeholder="关联的数据采集ID"></el-input>
                </el-form-item>
                <el-form-item label="清洗状态（如成功、失败）" prop="cleaningStatus">
                        <el-input v-model="dataForm.cleaningStatus" placeholder="清洗状态（如成功、失败）"></el-input>
                </el-form-item>
                <el-form-item label="清洗时间" prop="cleaningTime">
                        <el-date-picker
                                v-model="dataForm.cleaningTime"
                                type="datetime"
                                placeholder="清洗时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="原始数据" prop="originalData">
                        <el-input v-model="dataForm.originalData" placeholder="原始数据"></el-input>
                </el-form-item>
                <el-form-item label="清洗后的数据" prop="cleanedData">
                        <el-input v-model="dataForm.cleanedData" placeholder="清洗后的数据"></el-input>
                </el-form-item>
                <el-form-item label="清洗失败时的错误信息" prop="errorMessage">
                        <el-input v-model="dataForm.errorMessage" placeholder="清洗失败时的错误信息"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            cleaningId: null,
            collectionId: null,
            cleaningStatus: null,
            cleaningTime: null,
            originalData: null,
            cleanedData: null,
            errorMessage: null
          },
          dataRule: {
            cleaningId: [
                        { required: true, message: '数据清洗ID不能为空', trigger: 'blur' }
            ],
            collectionId: [
                        { required: true, message: '关联的数据采集ID不能为空', trigger: 'blur' }
            ],
            cleaningStatus: [
                        { required: true, message: '清洗状态（如成功、失败）不能为空', trigger: 'blur' }
            ],
            cleaningTime: [
                        { required: true, message: '清洗时间不能为空', trigger: 'blur' }
            ],
            originalData: [
                        { required: true, message: '原始数据不能为空', trigger: 'blur' }
            ],
            cleanedData: [
                        { required: true, message: '清洗后的数据不能为空', trigger: 'blur' }
            ],
            errorMessage: [
                        { required: true, message: '清洗失败时的错误信息不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.cleaningId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.cleaningId = id || 0
            if (this.dataForm.cleaningId) {
              this.$http({
                url: this.$http.adornUrl(`/app/data_cleaning/info/${this.dataForm.cleaningId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appdatacleaning
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/data_cleaning/${!this.dataForm.cleaningId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>